//
//  CNCollectionView.swift
//  Voice
//
//  Created by 卢逢卫 on 2024/3/8.
//

import UIKit

extension UICollectionView {
    
    public static func cn_initWith(background: UIColor = .white, direction: UICollectionView.ScrollDirection = .vertical, itemSize: CGSize, lineSpacing: CGFloat, interitemSpacing: CGFloat) -> UICollectionView {
        let layout = UICollectionViewFlowLayout()
        layout.scrollDirection = direction
        layout.itemSize = itemSize
        layout.minimumLineSpacing = lineSpacing
        layout.minimumInteritemSpacing = interitemSpacing
        
        let collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout)
        collectionView.backgroundColor = background
        
        return collectionView
    }
    
    public func cn_registerCell(cls: AnyClass) {
        let identifier = String(describing: cls)
        self.register(cls, forCellWithReuseIdentifier: identifier)
    }
    
    public func cn_registerCell(cls: AnyClass, withIdentifier identifier: String) {
        self.register(cls, forCellWithReuseIdentifier: identifier)
    }
    
    public func cn_dequeueReusableCell<T>(cls: T.Type, for indexPath: IndexPath) -> T where T: UICollectionViewCell {
        let identifier = String(describing: cls)
        guard let cell = self.dequeueReusableCell(withReuseIdentifier: identifier, for: indexPath) as? T else {
            fatalError("Unable to dequeue reusable cell with identifier: \(identifier)")
        }
        return cell
    }
    
    public func cn_dequeueReusableCell<T>(withIdentifier identifier: String, for indexPath: IndexPath) -> T where T: UICollectionViewCell {
        guard let cell = self.dequeueReusableCell(withReuseIdentifier: identifier, for: indexPath) as? T else {
            fatalError("Unable to dequeue reusable cell with identifier: \(identifier)")
        }
        return cell
    }
    
    public func cn_registerHeader(cls: AnyClass) {
        let identifier = String(describing: cls)
        self.register(cls, forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: identifier)
    }
    
    public func cn_registerHeader(cls: AnyClass, withIdentifier identifier: String) {
        self.register(cls, forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: identifier)
    }
    
    public func cn_dequeueReusableHeader<T>(cls: T.Type, for indexPath: IndexPath) -> T where T: UICollectionReusableView {
        let identifier = String(describing: cls)
        guard let view = self.dequeueReusableSupplementaryView(ofKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: identifier, for: indexPath) as? T else {
            fatalError("Unable to dequeue reusable header with identifier: \(identifier)")
        }
        return view
    }
    
    public func cn_dequeueReusableHeader<T>(withIdentifier identifier: String, for indexPath: IndexPath) -> T where T: UICollectionReusableView {
        guard let view = self.dequeueReusableSupplementaryView(ofKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: identifier, for: indexPath) as? T else {
            fatalError("Unable to dequeue reusable header with identifier: \(identifier)")
        }
        return view
    }
    
    public func cn_registerFooter(cls: AnyClass) {
        let identifier = String(describing: cls)
        self.register(cls, forSupplementaryViewOfKind: UICollectionView.elementKindSectionFooter, withReuseIdentifier: identifier)
    }
    
    public func cn_registerFooter(cls: AnyClass, withIdentifier identifier: String) {
        self.register(cls, forSupplementaryViewOfKind: UICollectionView.elementKindSectionFooter, withReuseIdentifier: identifier)
    }
    
    public func cn_dequeueReusableFooter<T>(cls: T.Type, for indexPath: IndexPath) -> T where T: UICollectionReusableView {
        let identifier = String(describing: cls)
        guard let view = self.dequeueReusableSupplementaryView(ofKind: UICollectionView.elementKindSectionFooter, withReuseIdentifier: identifier, for: indexPath) as? T else {
            fatalError("Unable to dequeue reusable footer with identifier: \(identifier)")
        }
        return view
    }
    
    public func cn_dequeueReusableFooter<T>(withIdentifier identifier: String, for indexPath: IndexPath) -> T where T: UICollectionReusableView {
        guard let view = self.dequeueReusableSupplementaryView(ofKind: UICollectionView.elementKindSectionFooter, withReuseIdentifier: identifier, for: indexPath) as? T else {
            fatalError("Unable to dequeue reusable footer with identifier: \(identifier)")
        }
        return view
    }
}

//extension UISwitch {
//    
//    public static func cn_initWith(isOn: Bool, onColor: UIColor?, thumbColor: UIColor?, scale: CGFloat) -> UISwitch {
//        let uiSwitch = UISwitch()
//        uiSwitch.isOn = isOn
//        uiSwitch.onTintColor = onColor
//        uiSwitch.thumbTintColor = thumbColor
//        uiSwitch.transform = CGAffineTransform(scaleX: scale, y: scale)
//        return uiSwitch
//    }
//}
